﻿#include "stdafx.h"
#include "defines.h"
#include "globals.h"

#ifdef __cplusplus
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath);
#endif


NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath)
{
	PDEVICE_OBJECT DeviceObject = (PDEVICE_OBJECT)ExAllocatePool(NonPagedPool,sizeof(DEVICE_OBJECT));
	if (DeviceObject==NULL)
	{
		DbgPrint("Sorry,Non-paged Memory is missing...Non-paged memory available is less than %d Bytes!",sizeof(DEVICE_OBJECT));
		return STATUS_UNSUCCESSFUL;
	}
	NTSTATUS status;

	DriverObject->MajorFunction[IRP_MJ_CREATE] = &FBFSCreate;
	DriverObject->MajorFunction[IRP_MJ_CLOSE] = &FBFSClose;

	DriverObject->MajorFunction[IRP_MJ_READ] = &FbinstRead;
	DriverObject->MajorFunction[IRP_MJ_WRITE] = &FbinstWrite;
	DriverObject->FastIoDispatch = (PFAST_IO_DISPATCH)ExAllocatePoolWithTag(NonPagedPool,sizeof(FAST_IO_DISPATCH),'fio');
	DriverObject->FastIoDispatch->SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
	DriverObject->FastIoDispatch->FastIoRead = &FbinstFastIoRead;
	DriverObject->FastIoDispatch->FastIoWrite = &FbinstFastIoWrite;
	DriverObject->FastIoDispatch->FastIoQueryBasicInfo = &FBFSFastQueryBasicInfo;
	DriverObject->FastIoDispatch->FastIoQueryStandardInfo = &FBFSFastQueryStandardInfo;
	DriverObject->FastIoDispatch->FastIoLock = &FBFSFastLock;
	DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = &FBFSFileSysControl;
	DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] = &FBFSDirectoryControl;
	DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = &FBFSQueryFileInfo;
	DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = &FBFSSetFileInfo;
	DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] = &FBFSLock;
	DriverObject->MajorFunction[IRP_MJ_PNP] = &FBFSPnP;
	status = IoCreateDevice(DriverObject,sizeof(FBFS_DRIVER_EXT),&usDeviceName,FILE_DEVICE_DISK_FILE_SYSTEM,0,FALSE,DeviceObject);
	if (!NT_SUCCESS(status))
		return status;
	if (!DeviceObject)
		return STATUS_UNEXPECTED_IO_ERROR;
	pDriverExtension = (FBFS_DRIVER_EXT*)DeviceObject->DeviceExtension;
	KeInitializeMutex(&pDriverExtension->DiskListLock,0);
	KeInitializeMutex(&pDriverExtension->udListLock,0);
	pDriverExtension->DiskNum = EnmuDisk(&(pDriverExtension->DiskListHead));
	pDriverExtension->UDNum = EnmuUD(&(pDriverExtension->DiskListHead),&(pDriverExtension->udListHead));
	GetUDFileLists(&(pDriverExtension->udListHead));
	DeviceObject->Flags |= DO_DIRECT_IO;
	DeviceObject->AlignmentRequirement = FILE_BYTE_ALIGNMENT;
	status = IoCreateSymbolicLink(&usSymlinkName, &usDeviceName);
	if (!NT_SUCCESS(status))
		return status;
	DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
	IoRegisterFileSystem(DeviceObject);
	pDriverExtension->ControlDevice = DeviceObject;
	return STATUS_SUCCESS;
	/*IoGetDeviceObjectPointer()*/
}


